package com.demo2do.boyle.backend.service.advertisement.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.boyle.backend.service.advertisement.AdvertisementService;
import com.demo2do.boyle.common.entity.advertisement.Advertisement;
import com.demo2do.boyle.common.entity.advertisement.Status;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.service.impl.GenericServiceImpl;
import com.demo2do.core.web.resolver.Page;

/**
 * 
 * @author leo
 */
@Service("advertisementService")
public class AdvertisementServiceImpl extends GenericServiceImpl<Advertisement> implements AdvertisementService {
	
	@Autowired 
	private GenericDaoSupport genericDaoSupport;
	
	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.AdvertisementService#count(com.demo2do.boyle.common.entity.advertisement.Status)
	 */
	public int count(Status status) {
		
		if(status == null) 
			status = Status.AUDITING;
		
		Map<String, Object> parameters = new HashMap<String, Object>(1);
		parameters.put("status", status);
		return genericDaoSupport.searchForInt("SELECT COUNT(1) FROM Advertisement WHERE status = :status", parameters);
	}

	/*
     * (non-Javadoc)
     * @see com.demo2do.boyle.backend.service.AdvertisementService#count(com.demo2do.core.persistence.support.Filter)
     */
	public int count(Filter filter) {
		return genericDaoSupport.searchForInt("SELECT COUNT(1) FROM Advertisement " + filter.getWhereSentence(), filter.getParameters());
	}

	/*
     * (non-Javadoc)
     * @see com.demo2do.boyle.backend.service.AdvertisementService#list(com.demo2do.core.web.resolver.Page)
     */
	@SuppressWarnings("unchecked")
	public List<Advertisement> list(Page page) {
		List<Advertisement> advertisements = genericDaoSupport.searchForList("FROM Advertisement", page.getBeginIndex(), page.getMaxResultRecords());
		page.calculate(advertisements.size());
		return advertisements.size() > page.getEveryPage() ? advertisements.subList(0, page.getEveryPage()) : advertisements;
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.AdvertisementService#list(com.demo2do.core.web.resolver.Page, com.demo2do.boyle.common.entity.AuditStatus)
	 */
	@SuppressWarnings("unchecked")
	public List<Advertisement> list(Status status, Page page) {
		
		if(status == null)
			status = Status.AUDITING;
		
		List<Advertisement> advertisements = genericDaoSupport.searchForList("FROM Advertisement WHERE status = :status", "status",  status, page.getBeginIndex(), page.getMaxResultRecords() );
		page.calculate(advertisements.size());
		return advertisements.size() > page.getEveryPage() ? advertisements.subList(0, page.getEveryPage()) : advertisements;
	}

	@Override
	public void updateClaimNumber(Advertisement advertisement) {
		//update claim_number of advertisement
		advertisement.setClaimNumber(advertisement.getClaimNumber() + 1);
		genericDaoSupport.update(advertisement);
	}
	
}
